package chap2_2;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author https://gitee.com/W_Jun
 * @version 1.0
 * @date 2021-11-25 23:32
 * @Description 字节流写数据的三种方式
 */
public class FileOutputStreamDemo02 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("chapter02-字节流\\fos.txt");
        //查看源码，可以发现FileOutputStream(String name)是执行了new File（String name）的,如下
        //FileOutputStream fos = new FileOutputStream(new File("chapter02-字节流\\fos.txt"));

        //File file = new File("chapter02-字节流\\fos.txt");
        //FileOutputStream fos2 = new FileOutputStream(file);
        //上面这两句其实可以写成一句，如下
        //FileOutputStream fos2 = new FileOutputStream(new File("chapter02-字节流\\fos.txt"));
        //由此可见，FileOutputStream(String name)和FileOutputStream(File file)其实底层做的事是一样的

        fos.write(97);
        fos.write(98);
        fos.write(99);
        fos.write(100);
        fos.write(101);

        byte[] bys = {97, 98, 99, 100, 101};
        fos.write(bys);

        //byte[] getBytes()：返回字符串对应的字节数组
        byte[] bytes = "abcde".getBytes();
        fos.write(bytes);

        fos.write(bys, 0, bytes.length);

        fos.close();
    }
}
